<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
  <META http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Admin Page For Online Token Sharing Game</title>
  <link link href="_/css/grids-min.css" rel="stylesheet" type="text/css" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <link link href="_/css/v3.css" rel="stylesheet" type="text/css" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <link link href="_/css/downloads.css" rel="stylesheet" type="text/css" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <link href='https://fonts.googleapis.com/css?family=Exo:700,700italic|Average' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" type="text/css" href="_/css/mystyle.css">
  <script type="text/javascript" src="_/js/jquery-1.8.2.min.js"></script>
  <script type="text/javascript" src="_/js/myscript.js"></script>
<?php
$realm = 'Restricted area';

//user => password
$users = array('admin' => 'mypass', 'guest' => 'guest');


if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Digest realm="'.$realm.
           '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');

    die('<div id="doc" style="width:760px; padding-left:0px; margin-left:0px">
	   <div id="bd">
	   		<div id="yui-main">
				<div class="yui-b">
					<div class="yui-g">
						<div class="pageHeading" style="padding:15px 10px 15px 0px;">Online Token Sharing Game !!</div>
                        <br/>
                        <div class="sectionHeading">Authentication Invalid</div>
						<div style="padding:0px 0px 15px 0px;">
							<p class="font4">You are not authorized to view this page. The username/password your have entered is incorrect.</p>
						</div>
					</div>
				</div>
	   		</div>
	   </div>
	   <div id="test">
	   </div>
	   </div>');
}


// analyze the PHP_AUTH_DIGEST variable
if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) ||
    !isset($users[$data['username']]))
	
    die('<div id="doc" style="width:760px; padding-left:0px; margin-left:0px">
	   	<div id="bd">
	   		<div id="yui-main">
				<div class="yui-b">
					<div class="yui-g">
						<div class="pageHeading" style="padding:15px 10px 15px 0px;">Online Token Sharing Game !!</div>
                        <br/>
                        <div class="sectionHeading">Authentication Invalid</div>
						<div style="padding:0px 0px 15px 0px;">
							<p class="font4">You are not authorized to view this page. The username/password your have entered is incorrect.</p>
						</div>
					</div>
				</div>
	   		</div>
	   </div>
	   <div id="test">
	   </div>
		</div>');


// generate the valid response
$A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);

if ($data['response'] != $valid_response)
    die('Wrong Credentials!');

// ok, valid username & password
echo 'You are logged in as: ' . $data['username'];


// function to parse the http auth header
function http_digest_parse($txt)
{
    // protect against missing data
    $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
    $data = array();
    $keys = implode('|', array_keys($needed_parts));

    preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);

    foreach ($matches as $m) {
        $data[$m[1]] = $m[3] ? $m[3] : $m[4];
        unset($needed_parts[$m[1]]);
    }

    return $needed_parts ? false : $data;
}
?>

</head>
<body>
<p> JMJ! </p>
  <?php require_once("facebook_include1.php");?>
<?php require_once("database.php");?>
<?php
    require_once("./config.php");
    $me = $facebook->api('/me');
    $access_token = $facebook->getAccessToken();
    $user = $facebook->getUser();
	
	
?>
<?php
login($access_token);
?>
</body>
</html>
